﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using TMHuy.config;

namespace TMHuy
{
    public class VillageInfo
    {
        public int lumber;
        public int clay;
        public int iron;
        public int crop;
        public int freeCop;
        public int maxStore;
        public int maxCrop;
        public ArrayList resFields;
        public ArrayList buildings;
        public ArrayList emptySlots;
        public ArrayList buildable;
        public int x;
        public int y;
        public Hashtable troops;
        public int waves;
        public ArrayList inBuilds;
        public bool underAttack;
        public int taskComplete;

        public Building getLowestCropland()
        {
            Building result = null;
            int minLevel = int.MaxValue;
            foreach (Building building in resFields)
            {
                if (building.resField && building.name.Equals("Cropland")&&minLevel>building.level)
                {
                    minLevel = building.level;
                    result = building;
                }
            }
            return result;
        }

        public Building getLowestWoodcutter()
        {
            Building result = null;
            int minLevel = int.MaxValue;
            foreach (Building building in resFields)
            {
                if (building.resField && building.name.Equals("Woodcutter") && minLevel > building.level)
                {
                    minLevel = building.level;
                    result = building;
                }
            }
            return result;
        }

        public Building getLowestClayPit()
        {
            Building result = null;
            int minLevel = int.MaxValue;
            foreach (Building building in resFields)
            {
                if (building.resField && building.name.Equals("Clay Pit") && minLevel > building.level)
                {
                    minLevel = building.level;
                    result = building;
                }
            }
            return result;
        }

        public Building getLowestIronMine()
        {
            Building result = null;
            int minLevel = int.MaxValue;
            foreach (Building building in resFields)
            {
                if (building.resField && building.name.Equals("Iron Mine") && minLevel > building.level)
                {
                    minLevel = building.level;
                    result = building;
                }
            }
            return result;
        }

        public bool buildBusy(bool plus)
        {
            if (plus)
            {
                if (Constants.tribe == 0)
                {
                    return (inBuilds.Count == 3);
                }
                else
                {
                    return (inBuilds.Count == 2);
                }
            }
            else
            {
                if (Constants.tribe == 0)
                {
                    return (inBuilds.Count == 2);
                }
                else
                {
                    return (inBuilds.Count == 1);
                }
            }
        }

        public int getTroopCount(string name)
        {
            if (troops[name] == null)
            {
                return 0;
            }
            else
            {
                return (int)troops[name];
            }
        }

        public int getLevel(string buildLink)
        {
            for (int i = 0; i < resFields.Count; i++)
            {
                Building res = (Building)resFields[i];
                if (res.buildLink.Equals(buildLink))
                {
                    return res.level;
                }
            }
            for (int i = 0; i < buildings.Count; i++)
            {
                Building res = (Building)buildings[i];
                if (res.buildLink.Equals(buildLink))
                {
                    return res.level;
                }
            }
            return -1;
        }

        public int getLevelByName(string name)
        {
            for (int i = 0; i < resFields.Count; i++)
            {
                Building res = (Building)resFields[i];
                if (res.name.Equals(name))
                {
                    return res.level;
                }
            }
            for (int i = 0; i < buildings.Count; i++)
            {
                Building res = (Building)buildings[i];
                if (res.name.Equals(name))
                {
                    return res.level;
                }
            }
            return -1;
        }
    }
}
